package com.example.chuqq.gpssigninapplication;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.example.chuqq.gpssigninapplication.controller.Rest;
import com.example.chuqq.gpssigninapplication.model.UserData;

import java.util.Calendar;
import java.util.Date;

public class MyLocationListener extends BDAbstractLocationListener {
    private static final String TAG = "MainService";

    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    private Context mContext;


    @Override
    public void onReceiveLocation(BDLocation location){
        // 如果当前时间无需上报，则忽略：工作日的
//        Date date = new Date();
//        Calendar calendar = Calendar.getInstance();
//        int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 0是周日，1是周一
//        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 22点就是22
//        if (hour < 8 || hour > 18) {
//            Log.d(TAG, "时间超出范围，不上报");
//            return;
//        }
//
        //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
        //以下只列举部分获取经纬度相关（常用）的结果信息
        //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

        double latitude = location.getLatitude();    //获取纬度信息
        double longitude = location.getLongitude();    //获取经度信息
        float radius = location.getRadius();    //获取定位精度，默认值为0.0f

        String coorType = location.getCoorType();
        //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

        int errorCode = location.getLocType();
        //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明

        Log.d(TAG, "onReceiveLocation: [" + latitude + ", " + longitude + "], radius: " + radius + ", coorType: " + coorType + ", errorCode: " + errorCode);

        UserData userData = new UserData(this.mContext);
        if (userData == null || userData.getPhone() == null || "".equals(userData.getPhone())) {
            return;
        }

        Rest.uploadGPS(userData, location.getLongitude(), location.getLatitude());
    }
}
